package com.example.springcloudexamineserver.service;

import com.example.springcloudexamineserver.config.WebSocketServer;
import com.example.springcloudexamineserver.entity.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExecutePython {
    public static String result;
    @Autowired
    Model model;

    @Autowired
    private WebSocketServer webSocketServer;

//    public String result = "";
    //调用数据清洗脚本
    public  void data_cleaning(String filepath) {
        try {
            System.out.println("data_cleaning start!");
//            String filepath = "D:\\PythonProject\\MNIST.onnx";
            String[] args1 = new String[]{"python", "C:\\Users\\CHQ\\PycharmProjects\\pythonProject\\yaozhicong-gai\\onnxToGraph.py", filepath};
            Process pr = Runtime.getRuntime().exec(args1);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    pr.getInputStream()
            ));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            pr.waitFor();
            System.out.println("data_cleaning end");
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }


    //调用Kerel算法计算相似度
    public void kerel(String filepath) {
        File file = new File(filepath);
        String[] filelist = file.list();
        List<String> list = new ArrayList<>();
        String cmd = "C:\\\\Users\\\\CHQ\\\\PycharmProjects\\\\pythonProject\\\\yaozhicong\\\\yaozhicong.py";
        String exe = "D:\\Python3.9.7\\python.exe";
        for(int i = 0;i < filelist.length;i++) {
            File readfile = new File(filepath + "\\" +filelist[i]);
            String gpipath = readfile.getPath();
            list.add(gpipath);
        }
        System.out.println(list);
        try {
            String[] args1 = new String[] {exe,cmd, String.valueOf(list)};
            Process process = Runtime.getRuntime().exec(args1);

            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            result = input.readLine();
            ir.close();
            System.out.println("执行结果" + result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
